import { getInfo, getUserDetailById } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    updataUserInfo(state, info) {
      state.userInfo = info
    },
    delUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getUserInfo(context) {
      const res = await getInfo()
      console.log('获取用户信息', res)
      const info = await getUserDetailById(res.data.userId)
      console.log('获取详细信息', info)
      context.commit('updataUserInfo', { ...res.data, ...info.data })
    }
  }
}
